package com.wandian.texas;

import com.wandian.util.ArrayList;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.TimeUnit;

import com.wandian.centerserver.CenterSessionMgr;
import com.wandian.executor.TimerCommand;
import com.wandian.texas.command.texas.CenterTimersCmd;
import com.wandian.texas.dao.Impl.GameRoomDaoImpl;
import com.wandian.texas.db.CommonDaoManager;
import com.wandian.texas.db.DBMgr;
import com.wandian.texas.db.DaoManager;
import com.wandian.texas.entity.GameRoomEntity;
import com.wandian.util.Log;

/**
 * @author long
 * @Create 2012-10-19 下午5:39:47
 * @QQ 108232706
 * @version 1.0
 * @description GameMgr
 */
public class GameMgr {

	private static Map<String, TimerCommand> timers = new HashMap<String, TimerCommand>();
	public static final ArrayList<GameRoomEntity> ErrorGame = new ArrayList<GameRoomEntity>();

	public static void addErrorGame(GameRoomEntity game) {
		if (ErrorGame.contains(game)) {
			throw new IllegalArgumentException(" has exist game,id:" + game.GameId);
		} else {
			ErrorGame.add(game);
		}
	}

	/**
	 * 游戏服所有定时器操作的启动
	 */
	public static void startAllTimer() {
		if (!ThreadPool.getInstance().isThreadPoolStated()) {
			Log.error("=======线程池没有启动,定时器不能启动====");
			return;
		}
		startSynNotSendMoney();
	}

	public static TimeUnit getTimeUnit(String strTime) {
		if ("SECONDS".equals(strTime)) {
			return TimeUnit.SECONDS;
		}
		if ("MINUTES".equals(strTime)) {
			return TimeUnit.MINUTES;
		}
		if ("HOURS".equals(strTime)) {
			return TimeUnit.HOURS;
		}
		if ("DAYS".equals(strTime)) {
			return TimeUnit.DAYS;
		}
		if ("MILLISECONDS".equals(strTime)) {
			return TimeUnit.MILLISECONDS;
		}
		if ("MICROSECONDS".equals(strTime)) {
			return TimeUnit.MICROSECONDS;
		}
		if ("NANOSECONDS".equals(strTime)) {
			return TimeUnit.NANOSECONDS;
		}
		throw new IllegalArgumentException("getTimeUnit error");
	}

	/**
	 * 游戏服没有发送成功的钱，需要定时的更新
	 */
	private static void startSynNotSendMoney() {
		TimerCommand timer = timers.get("SynNotSendMoney");
		if (timer != null)
			timer.setCancel(false);
		else {
			int minutes = Math.round(GameData.getFloat("NotSendMoneyTimer"));
			timer = new TimerCommand("SynNotSendMoney", CenterSessionMgr.CMD_CENTER_SERVER_TIMERS, minutes, minutes, -1, getTimeUnit(GameData.getString("NotSendMoneyTimer")));
			timer.put("Type", CenterTimersCmd.SYNNOTSENDMONEY);
			timers.put("SynNotSendMoney", timer);
		}
		ThreadPool.getInstance().Push(timer);
	}

	public static GameRoomDaoImpl GameRoomDao(TimerCommand cmd) {
		if (cmd == null)
			return CommonDaoManager.GameRoomDao(0);
		else {
			return DaoManager.GameRoomDao(cmd.dbIndex);
		}
	}

	static float lastSynNotSendMoney = 0;

	public static void SynNotSendMoney(TimerCommand cmd) {
		if (lastSynNotSendMoney == GameData.getSystemTotalWin())
			return;
		if (GameRoomDao(cmd).executeNoQuery("sp_UpdateNotSendMoney", DBMgr.TexasPokerDB, GameData.SERVER.ServerId, GameData.getSystemTotalWin()) == -1) {
			Log.error("同步未发送钱失败!");
		} else {
			lastSynNotSendMoney = GameData.getSystemTotalWin();
		}
	}

	public static void stopAllTimer() {
		if (!ThreadPool.getInstance().isThreadPoolStated()) {
			Log.error("=======线程池没有启动,定时器不需要停止====");
			return;
		}
		TimerCommand timer = timers.get("SynNotSendMoney");
		if (timer != null) {
			timer.setCancel(true);
			ThreadPool.getInstance().cancel(timer);
		}
		SynNotSendMoney(null);
	}
}
